package com.hxh.generater.vue.springboot3_vue3.gen;

import com.hxh.entity.Project;
import com.hxh.entity.TemplateConfig;
import com.hxh.entity.po.ClassInfo;
import com.hxh.generater.common.remote.VueRemoteUtil;
import com.hxh.util.GenUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;

@Slf4j
public class GenSb3Vue3ApiRemote {

    /**
     * 生成apiModel
     *
     * @param project
     * @param classInfo
     * @return
     * @throws Exception
     */
    private static String generationApiModel(Project project, ClassInfo classInfo, boolean writeFile) throws IOException {
        String entityName = StringUtils.uncapitalize(classInfo.getClassName());
        VueRemoteUtil vueRemoteUtil = new VueRemoteUtil();
        vueRemoteUtil.setProject(project);
        vueRemoteUtil.setClassInfo(classInfo);
        vueRemoteUtil.setWriteFile(writeFile);
        vueRemoteUtil.setRemoteUrlSuffix("vue/apiModel");
        String packageMutilDatasourceStr = GenUtil.getMutilDatasourceStrByConfig(project, classInfo, null, File.separator);
        vueRemoteUtil.setWriteFilePath("src\\api\\" + packageMutilDatasourceStr + entityName + "\\");
        vueRemoteUtil.setWriteFileName("model.ts");
        return vueRemoteUtil.process();
    }

    public static String generationApiModel(Project project, ClassInfo classInfo) throws Exception {
        return generationApiModel(project, classInfo, true);
    }

    public static String generationApiModel(ClassInfo classInfo, TemplateConfig templateConfig) throws Exception {
        Project project = new Project();
        project.setTemplateConfig(templateConfig);
        return generationApiModel(project, classInfo, false);
    }

    private static String generationApiIndex(Project project, ClassInfo classInfo, boolean writeFile) throws IOException {
        String entityName = StringUtils.uncapitalize(classInfo.getClassName());
        VueRemoteUtil vueRemoteUtil = new VueRemoteUtil();
        vueRemoteUtil.setProject(project);
        vueRemoteUtil.setClassInfo(classInfo);
        vueRemoteUtil.setWriteFile(writeFile);
        vueRemoteUtil.setRemoteUrlSuffix("vue/apiIndex");
        String packageMutilDatasourceStr = GenUtil.getMutilDatasourceStrByConfig(project, classInfo, null, File.separator);
        vueRemoteUtil.setWriteFilePath("src\\api\\" + packageMutilDatasourceStr + entityName + "\\");
        vueRemoteUtil.setWriteFileName("index.ts");
        return vueRemoteUtil.process();
    }

    public static String generationApiIndex(Project project, ClassInfo classInfo) throws IOException {
        return generationApiIndex(project, classInfo, true);
    }

    public static String generationApiIndex(ClassInfo classInfo, TemplateConfig templateConfig) throws Exception {
        Project project = new Project();
        project.setTemplateConfig(templateConfig);
        return generationApiIndex(project, classInfo, false);
    }

}
